package com.ny.zmb.openapi.chat;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmChatHouse;
import com.ny.zmb.openapi.entity.PmUserToken;
import com.ny.zmb.openapi.service.IPmChatHouseService;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("chat")
public class ChatHouseController extends EYinShengBase {
    @Resource
    private IPmChatHouseService pmChatHouseService;
    @Resource
    private ChatService chatService;

    private static final String FG = ",%,";

    /**
     * 发送消息
     * @param json
     * @return
     */
    @PostMapping("sendMsg")
    public Result sendMsg(@RequestBody JSONObject json){
        PmUserToken userToken = getUserToken();
        PmChatHouse house = this.pmChatHouseService.getById(json.getInteger("id"));
        if (null == house){
            return Result.fail("信息已过期");
        }
        house.setDelFlag(0);
        house.setUpdatetime(getTime());
        this.pmChatHouseService.updateById(house);
        String userids = house.getUserids();
        String userId = String.format(FG,userToken.getUserId());
        String sendUserId = userids.replaceAll(userId, "");
        chatService.sendInfo(json.toJSONString(),sendUserId.replaceAll(",",""));
        return Result.ok();
    }
    /**
     * 查看房间
     */
    @GetMapping("list")
    public Result list(){
        PmUserToken userToken = getUserToken();
        LambdaQueryWrapper<PmChatHouse> wrapper = Wrappers.lambdaQuery();
        Integer userId = userToken.getUserId();
        String userIds = String.format(FG,userId);
        wrapper.like(PmChatHouse::getUserids,userIds);
        wrapper.orderByDesc(PmChatHouse::getUpdatetime);
        List<PmChatHouse> list = pmChatHouseService.list(wrapper);
        if (list == null || list.size() == 0){
            return Result.ok(new ArrayList<>());
        }
        return Result.ok(list
                .stream()
                .filter(e->{
                    return !userId.equals(e.getDelFlag());
                })
                .collect(Collectors.toList()));
    }

    /**
     * 添加
     */
    @PostMapping("add")
    public Result add(PmChatHouse house){
        PmUserToken userToken = getUserToken();
        String userIds = String.format(FG,userToken.getUserId()) + String.format(FG,house.getUserids());
        LambdaQueryWrapper<PmChatHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmChatHouse::getUserids,userIds);
        PmChatHouse one = this.pmChatHouseService.getOne(wrapper);
        if (null != one){
            return Result.ok(one);
        }
        house.setUserids(userIds);
        house.setCreatetime(getTime());
        house.setUpdatetime(getTime());
        house.setDelFlag(0);
        this.pmChatHouseService.save(house);
        return Result.ok(house);
    }

    /**
     * 删除
     */
    @GetMapping("delete")
    public Result add(Integer id){
        PmChatHouse byId = this.pmChatHouseService.getById(id);
        if (null == byId){
            return Result.ok();
        }
        Integer delFlag = byId.getDelFlag();
        PmUserToken userToken = getUserToken();
        if (0 == delFlag){
            byId.setDelFlag(userToken.getUserId());
            this.pmChatHouseService.updateById(byId);
        }else if (!userToken.getUserId().equals(delFlag)){
            this.pmChatHouseService.removeById(id);
        }
        return Result.ok();
    }

    private Integer getTime(){
        long l = System.currentTimeMillis();
        Long result = l / 1000;
        return result.intValue();
    }
}
